package com.medication.controller;

import com.jfinal.core.Controller;
import com.medication.model.SecAdminUser;
import com.medication.utils.Currents;
import com.medication.utils.Md5Utils;

public class LoginController extends Controller {

	public void index() {
		render("login.html");
	}

	public void sign() {
		String userName = getPara("userName");
		// TODO md5
		String password = getPara("password");

		SecAdminUser adminUser = SecAdminUser.dao.findFirst(
				"select * from sec_admin_user where user_name =? and password =? ", userName, Md5Utils.Md5(password));
		// 管理员登录
		if (adminUser != null) {
			//是否有效
			if("1".equals(adminUser.getValid())){
				setSessionAttr(Currents.CURRENT_USER, adminUser);
				setAttr(Currents.CURRENT_USER, adminUser);
				Currents.setCurrentUser(adminUser);
				redirect("/admin/index");
			}else{
				setAttr("msg", "您的帐号已冻结，请联系管理员");
				render("login.html");
			}
		} else {
			setAttr("msg", "用户不存在或者用户名密码错误");
			render("login.html");
		}
	}

	public void loginOut() {
		setSessionAttr(Currents.CURRENT_USER, null);
		Currents.setCurrentUser(null);
		render("login.html");
	}

	public static void main(String[] args) {
		System.out.println(Md5Utils.Md5("123"));
	}

}
